﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace StrokeCenterSystem.Utils
{
	public partial class DataConvert {
		/// <summary>
		/// 数据转换类
		/// </summary>

		/// <summary>
		/// 时间类型格式化字符串,格式为 yyyy-MM-dd HH:mm:ss
		/// </summary>
		public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
		/// <summary>
		/// 时间类型格式化字符串,格式为 yyyy-MM-dd
		/// </summary>
		public const string DateTimeFormatShortDate = "yyyy-MM-dd";
		/// <summary>
		/// string型转换为bool型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <returns>转换后的bool类型结果</returns>
		public static bool StrToBool(string strValue) {
			if (!string.IsNullOrEmpty(strValue) && !Convert.IsDBNull(strValue)) {
				strValue = strValue.Trim();
				return (((string.Compare(strValue, "true", true) == 0) || (string.Compare(strValue, "yes", true) == 0)) || (string.Compare(strValue, "1", true) == 0));
			}
			return false;
		}
		/// <summary>
		/// object型转换为bool型
		/// </summary>
		/// <param name="obj">要转换的字符串</param>
		/// <returns>转换后的bool类型结果</returns>
		public static bool StrToBool(object obj) {
			return Convert.ToBoolean(obj);
		}
		/// <summary>
		/// string型转换为时间型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <param name="defValue">缺省值</param>
		/// <returns>转换后的时间类型结果</returns>
		public static DateTime StrToDateTime(object strValue, DateTime defValue) {
			if ((strValue == null) || Convert.IsDBNull(strValue) || (strValue.ToString().Length > 40)) {
				return defValue;
			}

			DateTime val;

			if (!DateTime.TryParse(strValue.ToString(), out val)) {
				val = defValue;
			}
			return val;
		}
		/// <summary>
		/// 将输入的int型字符串转换为DateTime类型
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public static DateTime StrIntToDateTime(string input) {
			return IntToDateTime(StrToInt(input, 0));
		}
		/// <summary>
		/// 将输入的字符串int型转换为DateTime类型
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public static DateTime StrIntToDateTime(object input) {
			return StrIntToDateTime(input.ToString());
		}
		/// <summary>
		/// 将输入的字符串int型转换为DateTime字符串类型
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public static string StrIntToStrDateTime(string input) {
			return StrIntToDateTime(StrToInt(input, 0)).ToString("yyyy-MM-dd HH:mm:ss");
		}
		/// <summary>
		/// 将输入的long型转换为DateTime型
		/// </summary>
		/// <param name="inputLong"></param>
		/// <returns></returns>
		public static DateTime IntToDateTime(int inputLong) {
			DateTime beginTime = DateTime.Now.Date;
			DateTime.TryParse("1970-01-01", out beginTime);
			double addDays = (double)inputLong / (double)(24 * 3600);
			beginTime = beginTime.ToLocalTime();
			return beginTime.AddDays((double)addDays);
		}
		/// <summary>
		/// 将输入的DateTime型转换为int型
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		public static int DateTimeToInt(DateTime time) {
			DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
			TimeSpan toNow = time.Subtract(dtStart);
			return (int)(toNow.Ticks / 10000000);
		}
		/// <summary>
		/// object型转换为decimal型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <returns>转换后的decimal类型结果</returns>
		public static decimal StrToDecimal(object strValue) {
			if (!Convert.IsDBNull(strValue) && !object.Equals(strValue, null)) {
				return StrToDecimal(strValue.ToString());
			}
			return 0M;
		}
		/// <summary>
		/// string型转换为decimal型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <returns>转换后的decimal类型结果</returns>
		public static decimal StrToDecimal(string strValue) {
			decimal num;
			decimal.TryParse(strValue, out num);
			return num;
		}
		/// <summary>
		/// string型转换为decimal型
		/// </summary>
		/// <param name="input">要转换的字符串</param>
		/// <param name="defaultValue">缺省值</param>
		/// <returns>转换后的decimal类型结果</returns>
		public static decimal StrToDecimal(string input, decimal defaultValue) {
			decimal num;
			if (decimal.TryParse(input, out num)) {
				return num;
			}
			return defaultValue;
		}
		/// <summary>
		/// string型转换为decimal型
		/// </summary>
		/// <param name="input">要转换的字符串</param>
		/// <param name="defaultValue">缺省值</param>
		/// <param name="pointLength">要保留小数点后面的位数</param>
		/// <returns>转换后的decimal类型结果</returns>
		public static decimal StrToDecimal(string input, decimal defaultValue, int pointLength) {
			if (input.Contains(".")) {
				int len = input.IndexOf(".") + (pointLength + 1);
				if (input.Length > len) {
					input = input.Substring(0, len);
				}
			}
			decimal num;
			if (!decimal.TryParse(input, out num)) {
				num = defaultValue;
			}
			return num;
		}
		/// <summary>
		/// string型转换为double型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <returns>转换后的double类型结果</returns>
		public static double StrToDouble(object strValue) {
			if (!Convert.IsDBNull(strValue) && !object.Equals(strValue, null)) {
				return StrToDouble(strValue.ToString());
			}
			return 0.0;
		}
		/// <summary>
		/// string型转换为double型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <returns>转换后的double类型结果</returns>
		public static double StrToDouble(string strValue) {
			double num;
			double.TryParse(strValue, out num);
			return num;
		}
		/// <summary>
		/// string型转换为float型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <param name="defValue">缺省值</param>
		/// <returns>转换后的float类型结果</returns>
		public static float StrToFloat(object strValue, float defValue) {
			if ((strValue == null) || Convert.IsDBNull(strValue) || (strValue.ToString() == string.Empty)) {
				return defValue;
			}

			float val = 0;
			if (float.TryParse(strValue.ToString(), out val)) {
				return val;
			}
			return defValue;
		}
		/// <summary>
		/// string型转换为int型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <returns>转换后的int类型结果.如果要转换的字符串是非数字,则返回0.</returns>
		public static int StrToInt(object strValue) {
			return StrToInt(strValue, 0);
		}
		/// <summary>
		/// string型转换为int型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <param name="defValue">缺省值</param>
		/// <returns>转换后的int类型结果</returns>
		public static int StrToInt(object strValue, int defValue) {
			if ((strValue == null) || Convert.IsDBNull(strValue) || (strValue.ToString() == string.Empty)) {
				return defValue;
			}

			string val = strValue.ToString();
			if (val.IndexOf(".") >= 0) {
				val = val.Split(new char[] { '.' })[0];
			}
			int intValue = 0;
			if (int.TryParse(val, out intValue)) {
				return intValue;
			}
			return defValue;
		}
		/// <summary>
		/// string型转换为Long型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <param name="defValue">缺省值</param>
		/// <returns>转换后的Long类型结果</returns>
		public static long StrToLong(object strValue) {
			return StrToLong(strValue, 0);
		}
		/// <summary>
		/// string型转换为Long型
		/// </summary>
		/// <param name="strValue">要转换的字符串</param>
		/// <param name="defValue">缺省值</param>
		/// <returns>转换后的int类型结果</returns>
		public static long StrToLong(object strValue, long defValue) {
			if ((strValue == null) || Convert.IsDBNull(strValue) || (strValue.ToString() == string.Empty)) {
				return defValue;
			}

			string val = strValue.ToString();
			if (val.IndexOf(".") >= 0) {
				val = val.Split(new char[] { '.' })[0];
			}
			long intValue = 0;
			if (long.TryParse(val, out intValue)) {
				return intValue;
			}
			return defValue;
		}
		#region NPOI转换Excel日期时间格式

		/// <summary>
		/// NPOI转换Excel日期时间格式,出错时返回当前时间
		/// </summary>
		/// <param name="_datetime"></param>
		/// <returns></returns>
		public static string NPOIParseDateTime(string _datetime) {
			string[] arr = _datetime.Split(new char[] { ' ' });
			string strDate = "";
			try {
				strDate = NPOIParseDate(arr[0]);

				if (arr.Length > 1) {
					strDate += " " + NPOIParseTime(arr[1]);
				}
			}
			catch {
				strDate = DateTime.Now.ToString();
			}
			return strDate;
		}
		/// <summary>
		/// 转换日期格式
		/// </summary>
		/// <param name="_date"></param>
		/// <returns></returns>
		private static string NPOIParseDate(string _date) {
			if (_date.IndexOf("/") > 0) {
				string tem = "";
				string[] arr = _date.Split(new char[] { '/' });
				for (int i = 0; i < arr.Length; i++) {
					string str = arr[i];
					if (i == 2) {
						if (str.Length < 3) {
							tem += "20" + str.PadLeft(2, '0');
						}
						else {
							tem += str.PadLeft(2, '0');
						}
					}
					else {
						tem += str.PadLeft(2, '0');
					}
				}
				_date = tem;
			}


			//這裡定義所有日期格式 
			string[] dateFormats = new string[] { "MMddyy", "MMddyyyy", "MM/dd/yy","yyyy/MM/dd", "yyy/MM/dd", "yy/MM/dd", "y/MM/dd", 
           "yyyy-MM-dd", "yyy-MM-dd", "yy-MM-dd", "y-MM-dd", 
           "yyyy/M/dd","yyy/M/dd","yy/M/dd","y/M/dd", 
           "yyyy-M-dd","yyy-M-dd","yy-M-dd","y-M-dd", 
           "yyyy/MM/d","yyy/MM/d","yy/MM/d","y/MM/d", 
           "yyyy-MM-d","yyy-MM-d","yy-MM-d","y-MM-d", 
           "yyyy/M/d","yyy/M/d","yy/M/d","y/M/d", 
           "yyyy-M-d","yyy-M-d","yy-M-d","y-M-d" };
			try {
				//這裡來處理傳入的格式是否為日期格式，只需要簡單一行 
				DateTime datetime = DateTime.ParseExact(_date, dateFormats, null, DateTimeStyles.AllowWhiteSpaces);
				return datetime.ToString("yyyy-MM-dd");
			}
			catch (Exception) {
				return "";
			}
		}
		/// <summary>
		/// 转换时间格式
		/// </summary>
		/// <param name="_time"></param>
		/// <returns></returns>
		private static string NPOIParseTime(string _time) {
			string[] timeFormats = new string[]{ "HH:mm:ss","HH:mm","H:m", "HH:mm:s","HH:m:ss", "HH:m:s", 
           "H:mm:ss","H:mm:s","H:m:ss","H:m:s"};
			try {
				DateTime datetime = DateTime.ParseExact(_time, timeFormats, null, DateTimeStyles.AllowWhiteSpaces);
				return datetime.ToString("HH:mm:ss");
			}
			catch (Exception) {
				return "";
			}
		}

		#endregion
		/// <summary>
		/// 金额转大写
		/// </summary>
		/// <remarks>只支持三位小数转换,多余位数四舍五入</remarks>
		/// <param name="input">输入金额</param>
		/// <returns>中文大写金额</returns>
		public static string StrToChinese(string input) {
			if (string.IsNullOrEmpty(input))
				throw new ArgumentOutOfRangeException("input");
			string temp = string.Empty;

			bool IsNegative = false; // 是否是负数

			if (input.Trim().Substring(0, 1) == "-") {
				// 是负数则先转为正数
				input = input.Trim().Remove(0, 1);
				IsNegative = true;
			}

			input.Replace(",", "");//移除美式金额写法的逗号
			if (input.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length > 3)
				throw new ArgumentException("输入金额不合法！", "input");

			string strLower = null;
			string strUpart = null;
			string strUpper = null;

			int iTemp = 0;
			// 保留三位小数 123.4888→123.489　　123.4→123.4
			input = Math.Round(double.Parse(input), 3).ToString();
			if (input.IndexOf(".") > 0) {
				if (input.IndexOf(".") == input.Length - 3) {
					input = input + "00";
				}
			}
			else {
				input = input + ".000";
			}
			strLower = input;
			iTemp = 1;
			strUpper = "";
			while (iTemp <= strLower.Length) {
				switch (strLower.Substring(strLower.Length - iTemp, 1)) {
					case ".":
						strUpart = "圆";
						break;
					case "0":
						strUpart = "零";
						break;
					case "1":
						strUpart = "壹";
						break;
					case "2":
						strUpart = "贰";
						break;
					case "3":
						strUpart = "叁";
						break;
					case "4":
						strUpart = "肆";
						break;
					case "5":
						strUpart = "伍";
						break;
					case "6":
						strUpart = "陆";
						break;
					case "7":
						strUpart = "柒";
						break;
					case "8":
						strUpart = "捌";
						break;
					case "9":
						strUpart = "玖";
						break;
				}

				switch (iTemp) {
					case 1:
						strUpart = strUpart + "厘";
						break;
					case 2:
						strUpart = strUpart + "分";
						break;
					case 3:
						strUpart = strUpart + "角";
						break;
					case 4:
						strUpart = strUpart + "";
						break;
					case 5:
						strUpart = strUpart + "";
						break;
					case 6:
						strUpart = strUpart + "拾";
						break;
					case 7:
						strUpart = strUpart + "佰";
						break;
					case 8:
						strUpart = strUpart + "仟";
						break;
					case 9:
						strUpart = strUpart + "万";
						break;
					case 10:
						strUpart = strUpart + "拾";
						break;
					case 11:
						strUpart = strUpart + "佰";
						break;
					case 12:
						strUpart = strUpart + "仟";
						break;
					case 13:
						strUpart = strUpart + "亿";
						break;
					case 14:
						strUpart = strUpart + "拾";
						break;
					case 15:
						strUpart = strUpart + "佰";
						break;
					case 16:
						strUpart = strUpart + "仟";
						break;
					case 17:
						strUpart = strUpart + "万";
						break;
					default:
						strUpart = strUpart + "";
						break;
				}

				strUpper = strUpart + strUpper;
				iTemp = iTemp + 1;
			}

			strUpper = strUpper.Replace("零拾", "零");
			strUpper = strUpper.Replace("零佰", "零");
			strUpper = strUpper.Replace("零仟", "零");
			strUpper = strUpper.Replace("零零零", "零");
			strUpper = strUpper.Replace("零零", "零");
			strUpper = strUpper.Replace("零角零分零厘", "整");
			//strUpper = strUpper.Replace("角零分", "角");
			//strUpper = strUpper.Replace("零角", "零");
			strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
			strUpper = strUpper.Replace("亿零万零圆", "亿圆");
			strUpper = strUpper.Replace("零亿零万", "亿");
			strUpper = strUpper.Replace("零万零圆", "万圆");
			strUpper = strUpper.Replace("零亿", "亿");
			strUpper = strUpper.Replace("零万", "万");
			strUpper = strUpper.Replace("零圆", "圆");
			strUpper = strUpper.Replace("零零", "零");

			// 对壹圆以下的金额的处理
			if (strUpper.Substring(0, 1) == "圆") {
				strUpper = strUpper.Substring(1, strUpper.Length - 1);
			}
			if (strUpper.Substring(0, 1) == "零") {
				strUpper = strUpper.Substring(1, strUpper.Length - 1);
			}
			if (strUpper.Substring(0, 1) == "角") {
				strUpper = strUpper.Substring(1, strUpper.Length - 1);
			}
			if (strUpper.Substring(0, 1) == "分") {
				strUpper = strUpper.Substring(1, strUpper.Length - 1);
			}
			if (strUpper.Substring(0, 1) == "厘") {
				strUpper = strUpper.Substring(1, strUpper.Length - 1);
			}
			if (strUpper.Substring(0, 1) == "整") {
				strUpper = "零圆整";
			}
			temp = strUpper;

			if (IsNegative == true) {
				return "负" + temp;
			}
			else {
				return temp;
			}
		}
		///// <summary>
		///// 金额转大写
		///// </summary>
		///// <param name="input">输入金额</param>
		///// <returns>中文大写金额</returns>
		//public static string DecimalToChinese(decimal input) { return StrToChinese(input.ToString()); }

		public static string DecimalToChinese(decimal number) {
			var s = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
			var d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
			var r = Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
			return r;
		}

        /// <summary>
        /// 转换为string类型 defult为string.Empty
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString(object obj)
        {
            return ZConvert.To<string>(obj, string.Empty);
        }
        /// <summary>
        /// 阿拉伯数字到罗马转换
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static string GetLoumaNumber(int n)
        {
            int[] arabic = new int[13];
            string[] roman = new string[13];
            int i = 0;
            string o = "";

            arabic[0] = 1000;
            arabic[1] = 900;
            arabic[2] = 500;
            arabic[3] = 400;
            arabic[4] = 100;
            arabic[5] = 90;
            arabic[6] = 50;
            arabic[7] = 40;
            arabic[8] = 10;
            arabic[9] = 9;
            arabic[10] = 5;
            arabic[11] = 4;
            arabic[12] = 1;

            roman[0] = "M";
            roman[1] = "CM";
            roman[2] = "D";
            roman[3] = "CD";
            roman[4] = "C";
            roman[5] = "XC";
            roman[6] = "L";
            roman[7] = "XL";
            roman[8] = "X";
            roman[9] = "IX";
            roman[10] = "V";
            roman[11] = "IV";
            roman[12] = "I";

            while (n > 0)
            {
                while (n >= arabic[i])
                {
                    n = n - arabic[i];
                    o = o + roman[i];
                }
                i++;
            }
            return o;
        }
	}
}
